<?php
/**
 *	Shows an Exception and quits.
 *	@package		mv2.core.exception
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			22.04.2007
 *	@version		0.1
 */
/**
 *	Shows an Exception and quits.
 *	@package		mv2.core.exception
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			22.04.2007
 *	@version		0.1
 */
class FatalExceptionHandler
{
	/**	@var		string		$break			Line Break Symbol */
	var $break		= "\n";
	/**	@var		string		$line			Line Symbol */
	var $line		= "";
	/**	@var		string		$template		File Name of Template to load */
	var $template	= "contents/fatalException.phpt";

	/**
	 *	Constructur, shows Exception and quits.
	 *	@access		public
	 *	@return		void
	 */
	public function __construct( $e )
	{
		$html	= !getEnv( 'PROMPT' ) && !getEnv( 'SHELL' );
		$this->break	= $html ? "<br/>\n" : "\n";
		$this->line		= $html ? "<hr/>\n" : str_repeat( "-", 70 ).$this->break;
		
		if( !$html )
			die( $this->showException( $e ) );

		ob_start();
		$this->showException( $e );	
		$source	= ob_get_clean();
		$source	= str_replace( "<hr/>", str_repeat( "-", 70 ), $source );
		ob_start();
		$this->showException( $e, "error" );	
		$error	= ob_get_clean();
		ob_start();
		$this->showException( $e, "trace" );	
		$trace	= ob_get_clean();
		$trace	= str_replace( getCwd(), "", $trace );

		$error	= str_replace( ": ", "</td><td>", $error );
		$error	= str_replace( "<br/>", "</td></tr>\n  <tr><td>", $error );
		$error	= "<table class='grid'>\n  <tr><td>".$error."</td></tr>\n</table>";
		$error	= str_replace( "<tr><td>\n</td></tr>", "", $error );
		
		ob_start();
		$this->showLog( "logs/database.log", 5 );
		$dbLog	= ob_get_clean();

		ob_start();
		$this->showLog( "logs/autoload.log", 5 );
		$load	= ob_get_clean();

		$body	= require_once( $this->template );
		die( $body );
	}
	
	/**
	 *	Sets File Name of Template.
	 *	@access		public
	 *	@param		string		$template	Template to load
	 *	@return		void
	 */
	function setTemplate( $template )
	{
		$this->template	= $template;
	}

	/**
	 *	Shows Exception in Parts (Error and Trace).
	 *	@access		private
	 *	@param		Exception	$e			Exception
	 *	@param		string		$mode		Parts to show (both|error|trace)
	 *	@return		void
	 */
	private function showException( $e, $mode = "both" )
	{
		$trace	= $e->getTrace();
		$code	= $e->getCode();
		if( $mode != "trace" )
		{
			print( "Error: ".$e->getMessage().$this->break );
			print( "File: ".$e->getFile().$this->break );
			print( "Line: ".$e->getLine().$this->break );
			if( $code )
				print( "Code: ".$code.$this->break );
			if( $mode == "error" )
				return;
		}
		foreach( $trace as $data )
		{
			extract( $data );
			$class	= isset( $class ) ? $class : "";
			$type	= isset( $type ) ? $type : "";
			print( $this->line );
			print( $class.$type.$function.$this->break );
			print( $file." [".$line."]".$this->break );
		}
	}
	
	/**
	 *	Shows Lines of Log.
	 *	@access		private
	 *	@param		string		$filename	File Name of Log File
	 *	@param		int			$limit		Amount of Lines from the end
	 *	@return		void
	 */
	private function showLog( $filename, $limit )
	{
		$lines	= explode( "\n", trim( file_get_contents( $filename ) ) );
		if( $limit )
		{
			$lines	= array_slice( $lines, -1 * abs( $limit ) );
		}
		$lines	= implode( $this->break, $lines );
		$lines	= preg_replace( "@".$this->break."([0-9]+:)@si", $this->line."\\1", $lines );
		print( $lines );
	}
}
?>
